import { render, VNode } from "vue";
export const renderComponent = (vNode: VNode, traget: Element | ShadowRoot) => {
	render(vNode, traget);
};
/**
 *
 * @param target 目标对象
 * @param repaintCondition 条件
 * @param fn repaintCondition 为真才会执行 fn
 * @param time 定时器标识
 * @returns
 */
let time: null | NodeJS.Timeout = null;
const repaintProxy = <T extends object>(
	target: T,
	repaintCondition: boolean | ((key: string) => boolean),
	fn: Function
): T => {
	const condition =
		repaintCondition instanceof Function ? repaintCondition : () => repaintCondition;

	return new Proxy(target, {
		get(target, key: string, receiver) {
			const res: any = Reflect.get(target, key, receiver);
			if (condition(key)) {
				if (time) {
					clearTimeout(time);
					time = null;
				}
				time = setTimeout(() => {
					fn();
				}, 260);
				return res;
			}
			return res;
		},
	});
};

function signReplace(url: string, target: string, val: string) {
	const reg = new RegExp(`#${target}#`, "g");
	return url.replace(reg, val);
}

export { repaintProxy, signReplace };
